
package com.tools.cleanmaster.ui.card.ui;

import android.content.Context;
import android.text.Spanned;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.hm.base.android.ui.view.CommonInfoView;
import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.AnimatorListenerAdapter;
import com.tools.cleanmaster.CleanApplication;
import com.tools.cleanmaster.R;
import com.tools.cleanmaster.ui.card.animator.AnimatorHelper;
import com.tools.cleanmaster.ui.card.factory.CardListAdapter;
import com.tools.cleanmaster.ui.view.oversroll.OverScrollListView;

public class ResultMultiCardLayout extends CommonInfoView<CleanApplication> {

    public interface ResultCardAnimListener {
        void onAllAnimationStart();
        void onHeaderAnimationStart();
        void onAllAnimationEnd();
    }

    private Context mContext;
    private View mListViewHeader;
    private TextView mListHeadTitle;
    private ImageView mHeaderImageView;
    private OverScrollListView mCardListView;

    public ResultMultiCardLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        addView(R.layout.result_card_view);
    }

    public ResultMultiCardLayout(Context context) {
        super(context);
        mContext = context;
        addView(R.layout.result_card_view);
        initViews(this);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        initViews(this);
    }

    private void initViews(View view) {
        mCardListView = view.findViewById(R.id.card_list_view);
        mCardListView.setVerticalFadingEdgeEnabled(false);
        mCardListView.setFadingEdgeLength((int) getResources().getDimension(
                R.dimen.list_fading_edge));
        mCardListView.setForbidOverScrollTop(true);
    }

    /**
     * 设置头部view
     *
     * @param view
     */
    public void setHeaderView(View view) {
        if (view == null) {
            return;
        }
        mListViewHeader = view;
        mListHeadTitle = mListViewHeader.findViewById(R.id.resultcard_header_title);
        mHeaderImageView = mListViewHeader.findViewById(R.id.resultcard_header_portrait);
        mCardListView.addHeaderView(mListViewHeader);
    }

    public void startTipsAnim() {
        AnimatorHelper.startTipsArrowAnim(mContext, null);
    }

    /**
     * 设置卡片数据并伴随title及上面的箭头动画
     *
     * @param adapter
     */
    public void setCardAdapterWithAnim(CardListAdapter adapter) {
        setCardAdapterWithAnim(adapter, null);
    }

    /**
     * 设置卡片数据并伴随title及上面的箭头动画
     *
     * @param adapter
     */
    public void setCardAdapterWithAnim(final CardListAdapter adapter,
                                       final ResultCardAnimListener listener) {
        mCardListView.setVerticalScrollBarEnabled(false);
        mCardListView.setAdapter(adapter);
        mCardListView.post(new Runnable() {

            @Override
            public void run() {
                mCardListView.setVisibility(View.VISIBLE);
                AnimatorHelper.doListViewAnimation(mCardListView, mListViewHeader,
                        new AnimatorHelper.ListItemAnimListener() {
                            @Override
                            public void onAnimationEnd() {
                                mCardListView.clearAnimation();
                                if (listener != null) {
                                    listener.onAllAnimationEnd();
                                }
                                startHeadAnim();
                            }

                            @Override
                            public void onAnimationStart() {
                                if (listener != null) {
                                    listener.onAllAnimationStart();
                                }
                            }

                            @Override
                            public void onAnimationGoing() {
                                if (listener != null) {
                                    listener.onHeaderAnimationStart();
                                }
                            }
                        });
            }
        });
    }

    private void startHeadAnim() {
        AnimatorHelper.startHeaderViewAnim(mHeaderImageView, mListHeadTitle, 200, new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                startTipsAnim();
            }
        });
    }

    public void setHeaderChildViewInvisible() {
        if (mHeaderImageView != null) {
            mHeaderImageView.setVisibility(INVISIBLE);
        }
        if (mListHeadTitle != null) {
            mListHeadTitle.setVisibility(INVISIBLE);
        }
    }

    /**
     * 设置头文案
     *
     * @param title
     */
    public void setHeadTitle(Spanned title) {
        if (mListHeadTitle != null) {
            mListHeadTitle.setText(title);
        }
    }

    public void setHeadTitle(String title) {
        if (mListHeadTitle != null) {
            mListHeadTitle.setText(title);
        }
    }

    public ListView getCardListView() {
        return mCardListView;
    }
}
